package com.husd.framework.code;

import com.google.common.base.CaseFormat;
import com.husd.framework.ddl.AutoCodeUtil;
import com.husd.framework.ddl_new.CheckConstraintDefinition;
import org.apache.commons.lang3.StringUtils;

public class DDLColumn {
    /**
     * 列名字
     */
    private String columnName;
    /**
     * 列声明的长度
     */
    private Integer columnLen;
    /**
     * 驼峰名字
     */
    private String columnNameLowerCamel;
    /**
     * 列类型 对应的java类型
     */
    private String columnType;
    /**
     * 是否是主键ID
     */
    private boolean id = false;
    /**
     * 注释
     */
    private String comment;
    /**
     * 默认值
     */
    private String defaultVal;
    /**
     * 是否是not null
     */
    private boolean notNull;

    /**
     * 列的长度
     */
    private int length;

    private String collationName;

    private String columnFormat;

    private String engineAttribute;

    private String secondaryEngineAttribute;

    private String storage;

    private ReferenceDefinition referenceDefinition;

    private String generateAlwaysAs;

    private String virtual;
    private String stored;

    private boolean visible;

    private boolean unique;

    private boolean primary;

    private CheckConstraintDefinition checkConstraintDefinition;

    public CheckConstraintDefinition getCheckConstraintDefinition() {
        return checkConstraintDefinition;
    }

    public void setCheckConstraintDefinition(CheckConstraintDefinition checkConstraintDefinition) {
        this.checkConstraintDefinition = checkConstraintDefinition;
    }

    public boolean isPrimary() {
        return primary;
    }

    public void setPrimary(boolean primary) {
        this.primary = primary;
    }

    public boolean isUnique() {
        return unique;
    }

    public void setUnique(boolean unique) {
        this.unique = unique;
    }

    public boolean isVisible() {
        return visible;
    }

    public void setVisible(boolean visible) {
        this.visible = visible;
    }

    public String getVirtual() {
        return virtual;
    }

    public void setVirtual(String virtual) {
        this.virtual = virtual;
    }

    public String getStored() {
        return stored;
    }

    public void setStored(String stored) {
        this.stored = stored;
    }

    public String getGenerateAlwaysAs() {
        return generateAlwaysAs;
    }

    public void setGenerateAlwaysAs(String generateAlwaysAs) {
        this.generateAlwaysAs = generateAlwaysAs;
    }

    public ReferenceDefinition getReferenceDefinition() {
        return referenceDefinition;
    }

    public void setReferenceDefinition(ReferenceDefinition referenceDefinition) {
        this.referenceDefinition = referenceDefinition;
    }

    public String getStorage() {
        return storage;
    }

    public void setStorage(String storage) {
        this.storage = storage;
    }

    public String getSecondaryEngineAttribute() {
        return secondaryEngineAttribute;
    }

    public void setSecondaryEngineAttribute(String secondaryEngineAttribute) {
        this.secondaryEngineAttribute = secondaryEngineAttribute;
    }

    public String getEngineAttribute() {
        return engineAttribute;
    }

    public void setEngineAttribute(String engineAttribute) {
        this.engineAttribute = engineAttribute;
    }

    public String getColumnFormat() {
        return columnFormat;
    }

    public void setColumnFormat(String columnFormat) {
        this.columnFormat = columnFormat;
    }

    public String getCollationName() {
        return collationName;
    }

    public void setCollationName(String collationName) {
        this.collationName = collationName;
    }

    public int getLength() {
        return length;
    }

    public void setLength(int length) {
        this.length = length;
    }

    public String getAttributeName() {

        return this.columnName;
    }

    public String getAttributeType() {

        return this.columnType;
    }

    public String getAttributeComment() {

        return this.comment;
    }

    public String getDefaultVal() {
        return defaultVal;
    }

    public void setDefaultVal(String defaultVal) {
        this.defaultVal = defaultVal;
    }

    public boolean isNotNull() {
        return notNull;
    }

    public void setNotNull(boolean notNull) {
        this.notNull = notNull;
    }

    public String getColumnNameLowerCamel() {
        return columnNameLowerCamel;
    }

    public void setColumnNameLowerCamel(String columnNameLowerCamel) {
        this.columnNameLowerCamel = columnNameLowerCamel;
    }

    public String getComment() {

        if(StringUtils.isBlank(this.comment)) {
            return this.columnName;
        }
        return comment;
    }

    public void setComment(String comment) {

        this.comment = comment.replaceAll("'", "");
    }

    public String getColumnName() {
        return columnName;
    }

    public void setColumnName(String columnName) {
        this.columnName = columnName;
        this.columnNameLowerCamel = CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, columnName);
    }

    public String getColumnType() {
        return columnType;
    }

    public String getColumnType4Java() {

        return AutoCodeUtil.getJavaType(columnType);
    }

    public void setColumnType(String columnType) {
        // 如果包含左括号，就处理下
        if (columnType.contains("(")) {
            columnType = columnType.substring(0, columnType.indexOf("("));
        }
        this.columnType = columnType;
    }

    public boolean isId() {
        return id;
    }

    public void setId(boolean id) {
        this.id = id;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("DDLColumn{");
        sb.append("columnName='").append(columnName).append('\'');
        sb.append(", columnType=").append(columnType);
        sb.append(", id=").append(id);
        sb.append(", comment='").append(comment).append('\'');
        sb.append('}');
        return sb.toString();
    }

    public Integer getColumnLen() {
        return columnLen;
    }

    public void setColumnLen(Integer columnLen) {
        this.columnLen = columnLen;
    }

    public String getAttributeNameFirstUpper() {

        return JavaAutoCodeUtil.firstCharUpper(getAttributeName());
    }
}
